package com.myth.safety.synchronized1;

/**
 * 使用同步方法解决实现Runnable接口的线程安全问题
 *
 *
 */
class Window3 implements Runnable{

    private int ticket = 100;
    Object obj = new Object();

    @Override
    public void run() {
        while (true) {
            show();
        }
    }
    public synchronized void show() { //同步监规器: this

        if (ticket > 0) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + ":卖票，票号为:" + ticket);
            ticket--;
        }
    }
}

public class WindowMethodTest3 {
    public static void main(String[] args) {
        Window3 w = new Window3();

        Thread t1 = new Thread(w);
        Thread t2 = new Thread(w);
        Thread t3 = new Thread(w);


        t1.setName("1号窗口");
        t2.setName("2号窗口");
        t3.setName("3号窗口");


        t1.start();
        t2.start();
        t3.start();


    }

}



